<template>
  <div class="container">
    <subject-table :list="list" @delByIndex="delByIndexFn"></subject-table>
    <!-- 2.2 父组件内， 通过@xxx接收自定义事件 -->
    <add-form @submit="onSubmit"></add-form>
  </div>
</template>

<script>
/*
  学习目标：完成新增一条数据
  步骤：
 
*/
import AddForm from "./components/AddForm.vue";
import SubjectTable from "./components/SubjectTable.vue";

export default {
  components: { SubjectTable, AddForm },
  name: "App",
  data() {
    return {
      list: [
        {
          id: 100,
          subject: "语文",
          score: 99,
          time: new Date("2010-08-12"),
        },
        {
          id: 101,
          subject: "数学",
          score: 34,
          time: new Date("2020-09-01"),
        },
        {
          id: 102,
          subject: "英语",
          score: 25,
          time: new Date("2018-11-22"),
        },
        {
          id: 103,
          subject: "体育",
          score: 100,
          time: new Date("2020-12-12"),
        },
      ],
    };
  },
  methods: {
    delByIndexFn(index) {
      console.log("index  ----->  ", index);
      this.list.splice(index, 1);
    },
    // 2.3. 父组件, 通过push添加新数据
    onSubmit(subject, score) {
      const newItem = {
        subject,
        score,
        id: Date.now(),
        time: new Date(),
      };
      this.list.push(newItem);
    },
  },
};
</script>

<style scoped></style>
